package com.pro_one.controller;

import com.pro_one.constant.MessageConstant;
import com.pro_one.entity.PageResult;
import com.pro_one.entity.QueryPageBean;
import com.pro_one.entity.Result;
import com.pro_one.pojo.Permission;
import com.pro_one.service.PermissionService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 动态菜单功能实现
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {
    @Reference
    private PermissionService permissionService;

    /**
     * 添加权限
     * @param permission
     * @return
     */
    @RequestMapping("/addPermission")
    public Result addPermission(@RequestBody Permission permission) {
        try {
            permissionService.addPermission(permission);
            return new Result(true,MessageConstant.ADD_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_MENU_FAIL);
        }


    }

    /**
     * 删除权限
     * @param id
     * @return
     */
    @RequestMapping("/deletePermission")
    public Result deletePermission(Integer id) {
        try {
            permissionService.deletePermission(id);
            return new Result(true,MessageConstant.DELETE_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_MENU_FAIL);
        }


    }

    /**
     * 修改数据
     * @param permission
     * @return
     */
    @RequestMapping("/updatePermission")
    public Result updatePermission(@RequestBody Permission permission) {
        try {
            permissionService.updatePermission(permission);
            return new Result(true,MessageConstant.UPDATE_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.UPDATE_MENU_FAIL);
        }


    }

    /**
     * 查询所有权限
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findAllPermission")
    public PageResult findAllPermission(@RequestBody QueryPageBean queryPageBean) {
        PageResult permissions = permissionService.findAllPermission(queryPageBean);
        return permissions;
    }

    /**
     * 把与指定角色关联的权限拍在前面并返回所有权限，主要用于回显数据
     * @param queryPageBean
     * @param roleId
     * @return
     */
    @RequestMapping("/findAllPermissionByRoleId")
    public PageResult findAllPermissionByRoleId(@RequestBody QueryPageBean queryPageBean,Integer roleId) {
        PageResult permissions = permissionService.findAllPermissionByRoleId(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(),roleId);
        return permissions;
    }

    /**
     * 根据角色id查询与之关联的所有权限id
     * @param roleId
     * @return
     */
    @RequestMapping("/findPermissionIdsByRoleId")
    public Integer[] findPermissionIdsByRoleId(Integer roleId) {
        Integer[] permissionIdsByRoleId = permissionService.findPermissionIdsByRoleId(roleId);
        return permissionIdsByRoleId;
    }

}
